package com.ttg.web.service.Impl;

import com.ttg.common.enums.AccountDimTypeEnum;
import com.ttg.common.enums.AccountItemTypeEnum;
import com.ttg.common.utils.BigDecimalUtils;
import com.ttg.common.utils.IntegerUtil;
import com.ttg.model.dto.CMAcctAnalysisDTO;
import com.ttg.model.pojo.CostManageAcctComprehensiveAnalysis;
import com.ttg.model.vo.CMAcctComprehensiveAnalysisVO;
import com.ttg.web.service.AcctComprehensiveAnalysisService;
import com.ttg.web.utils.AccountDimTypeUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/9/30 18:00
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
public class AcctComprehensiveAnalysisServiceImpl implements AcctComprehensiveAnalysisService {

    private static final int DIV_SCALE = 8;

    /**
     * 账户综合结构分解
     * @param reqDto 入参dto
     * @param settList 清算组数据
     * @param comparisonList 对照组数据
     * @param itemTypeEnum 项目类型枚举 1-综合付息，2-结算手续费,3-云音响费用,4-绩效和计价，5-涉贷利息差
     */
    @Override
    public CMAcctComprehensiveAnalysisVO comprehensiveComparisonNetting(CMAcctAnalysisDTO reqDto, List<CostManageAcctComprehensiveAnalysis> settList, List<CostManageAcctComprehensiveAnalysis> comparisonList, AccountItemTypeEnum itemTypeEnum) {
        Optional s1 = settList.stream().filter(o -> IntegerUtil.equals(itemTypeEnum.getValue(),o.getCmacItemType())).findFirst();
        CostManageAcctComprehensiveAnalysis settle = (CostManageAcctComprehensiveAnalysis)s1.get();
        Optional c1 = comparisonList.stream().filter(o -> IntegerUtil.equals(itemTypeEnum.getValue(),o.getCmacItemType())).findFirst();
        CostManageAcctComprehensiveAnalysis comparison = (CostManageAcctComprehensiveAnalysis)c1.get();
        AccountDimTypeEnum dimTypeEnum = AccountDimTypeEnum.getByVal(reqDto.getAcctDimType());

        CMAcctComprehensiveAnalysisVO vo = new CMAcctComprehensiveAnalysisVO();
        vo.setIntervalType(reqDto.getIntervalType());
        vo.setAcctDimType(reqDto.getAcctDimType());
        vo.setAcctItemType(itemTypeEnum.getValue());
        //清算年
        vo.setSettleYear(settle.getCmacYear());
        //清算月
        vo.setSettleMonth(settle.getCmacMonth());
        vo.setSettleIncomeAmt(settle.getCmacIncomeAmt());
        vo.setSettleOutAmt(settle.getCmacOutAmt());
        vo.setSettleNetOutAmt(settle.getCmacNetOutAmt());
        vo.setSettleAggregateOutAmt(settle.getCmacAggregateOutAmt());
        vo.setSettleRate(settle.getCmacRate());

        //对照年
        vo.setComparisonYear(comparison.getCmacYear());
        //对照月
        vo.setComparisonMonth(comparison.getCmacMonth());
        vo.setComparisonIncomeAmt(comparison.getCmacIncomeAmt());
        vo.setComparisonOutAmt(comparison.getCmacOutAmt());
        vo.setComparisonNetOutAmt(comparison.getCmacNetOutAmt());
        vo.setComparisonAggregateOutAmt(comparison.getCmacAggregateOutAmt());
        vo.setComparisonRate(comparison.getCmacRate());

        // 收入轧差
        BigDecimal nettingIncome = nettingIncome(settle,comparison,dimTypeEnum,itemTypeEnum);
        vo.setNettingIncomeAmt(nettingIncome);
        // 支出轧差
        BigDecimal nettingOut = nettingOut(settle,comparison,dimTypeEnum,itemTypeEnum);
        vo.setNettingOutAmt(nettingOut);
        // 净支出轧差
        BigDecimal nettingNetOut = nettingNetOut(dimTypeEnum,itemTypeEnum);
        vo.setNettingNetOutAmt(nettingNetOut);
        // 累计支出轧差
        BigDecimal nettingAggregateOutAmt = nettingAggregateOutAmt(settle,comparison,dimTypeEnum,itemTypeEnum);
        vo.setNettingAggregateOutAmt(nettingAggregateOutAmt);
        // 模拟利率轧差
        BigDecimal nettingRate = nettingRate(dimTypeEnum,itemTypeEnum);
        vo.setNettingRate(nettingRate);

        return vo;
    }

    /**
     * 收入轧差
     * 公式：
     * 综合付息：W1=0
     * 结算手续费：W2=R2-M2
     * 云音响费用：W3=0
     * 绩效和计价：W4=0
     * 涉贷利息差：W5=R5-M5
     */
    public BigDecimal nettingIncome(CostManageAcctComprehensiveAnalysis settle, CostManageAcctComprehensiveAnalysis comparison, AccountDimTypeEnum dimTypeEnum, AccountItemTypeEnum accountItemTypeEnum){
        BigDecimal result = null;
        switch (accountItemTypeEnum){
            case 综合付息:
                result = BigDecimal.ZERO;
                break;
            case 结算手续费:
                result = BigDecimalUtils.sub(settle.getCmacIncomeAmt(),comparison.getCmacIncomeAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"W2",result);
                break;
            case 云音响费用:
                result = BigDecimal.ZERO;
                break;
            case 绩效和计价:
                result = BigDecimal.ZERO;
                break;
            case 涉贷利息差:
                result = BigDecimalUtils.sub(settle.getCmacIncomeAmt(),comparison.getCmacIncomeAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"W5",result);
                break;
            default:
                result = BigDecimal.ZERO;
        }
        return result;
    }

    /**
     * 支出轧差
     * 公式：
     * 综合付息：X1 = Ls
     * 结算手续费：X2=S2-N2
     * 云音响费用：X3=S3-N3
     * 绩效和计价：X4=S4-N4
     * 涉贷利息差：0
     */
    public BigDecimal nettingOut(CostManageAcctComprehensiveAnalysis settle, CostManageAcctComprehensiveAnalysis comparison, AccountDimTypeEnum dimTypeEnum, AccountItemTypeEnum itemTypeEnum){
        BigDecimal result = null;
        switch (itemTypeEnum){
            case 综合付息:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"Ls");
                AccountDimTypeUtil.putResult(dimTypeEnum,"X1",result);
                break;
            case 结算手续费:
                result = BigDecimalUtils.sub(settle.getCmacOutAmt(),comparison.getCmacOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"X2",result);
                break;
            case 云音响费用:
                result = BigDecimalUtils.sub(settle.getCmacOutAmt(),comparison.getCmacOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"X3",result);
                break;
            case 绩效和计价:
                result = BigDecimalUtils.sub(settle.getCmacOutAmt(),comparison.getCmacOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"X4",result);
                break;
            case 涉贷利息差:
                result = BigDecimal.ZERO;
                AccountDimTypeUtil.putResult(dimTypeEnum,"X5",result);
                break;
            default:
                result = BigDecimal.ZERO;
        }
        return result;
    }

    /**
     * 净支出轧差
     * 公式：
     * 综合付息：Y1 = X1
     * 结算手续费：Y2=W2-X2
     * 云音响费用：Y3=X3
     * 绩效和计价：Y4=X4
     * 涉贷利息差：Y5=W5-X5
     */
    public BigDecimal nettingNetOut(AccountDimTypeEnum dimTypeEnum, AccountItemTypeEnum itemTypeEnum){
        BigDecimal result = null;
        switch (itemTypeEnum){
            case 综合付息:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"X1");
                break;
            case 结算手续费:
                result = BigDecimalUtils.sub(AccountDimTypeUtil.getResult(dimTypeEnum,"W2"),AccountDimTypeUtil.getResult(dimTypeEnum,"X2"));
                break;
            case 云音响费用:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"X3");
                break;
            case 绩效和计价:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"X4");
                break;
            case 涉贷利息差:
                result = BigDecimalUtils.sub(AccountDimTypeUtil.getResult(dimTypeEnum,"W5"),AccountDimTypeUtil.getResult(dimTypeEnum,"X5"));
                break;
            default:
                result = BigDecimal.ZERO;
        }
        return result;
    }

    /**
     * 累计支出轧差
     * 公式：
     * 综合付息：AA1 = Ls
     * 结算手续费：AA2=V2-Q2
     * 云音响费用：AA3=V3-Q3
     * 绩效和计价：AA4=V4-Q4
     * 涉贷利息差：AA5=V5-Q5
     */
    public BigDecimal nettingAggregateOutAmt(CostManageAcctComprehensiveAnalysis settle, CostManageAcctComprehensiveAnalysis comparison,AccountDimTypeEnum dimTypeEnum, AccountItemTypeEnum itemTypeEnum){
        BigDecimal result = null;
        switch (itemTypeEnum){
            case 综合付息:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"Ls");
                break;
            case 结算手续费:
                result = BigDecimalUtils.sub(settle.getCmacAggregateOutAmt(),comparison.getCmacAggregateOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"AA2",result);
                break;
            case 云音响费用:
                result = BigDecimalUtils.sub(settle.getCmacAggregateOutAmt(),comparison.getCmacAggregateOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"AA3",result);
                break;
            case 绩效和计价:
                result = BigDecimalUtils.sub(settle.getCmacAggregateOutAmt(),comparison.getCmacAggregateOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"AA4",result);
                break;
            case 涉贷利息差:
                result = BigDecimalUtils.sub(settle.getCmacAggregateOutAmt(),comparison.getCmacAggregateOutAmt());
                AccountDimTypeUtil.putResult(dimTypeEnum,"AA5",result);
                break;
            default:
                result = BigDecimal.ZERO;
        }
        return result;
    }

    /**
     * 模拟利率轧差
     * 公式：
     * 综合付息：Z1 = Ks
     * 结算手续费：Z2=AA2/Js*100%
     * 云音响费用：Z3=AA3/Js*100%
     * 绩效和计价：Z4=AA4/Js*100%
     * 涉贷利息差：Z5=AA5/Js*100%
     */
    public BigDecimal nettingRate(AccountDimTypeEnum dimTypeEnum, AccountItemTypeEnum itemTypeEnum){
        BigDecimal result = null;
        switch (itemTypeEnum){
            case 综合付息:
                result = AccountDimTypeUtil.getResult(dimTypeEnum,"Ks");
                break;
            case 结算手续费:
                result = BigDecimalUtils.mul(BigDecimalUtils.div(AccountDimTypeUtil.getResult(dimTypeEnum,"AA2"),AccountDimTypeUtil.getResult(dimTypeEnum,"Js"),DIV_SCALE),new BigDecimal(100));
                break;
            case 云音响费用:
                result = BigDecimalUtils.mul(BigDecimalUtils.div(AccountDimTypeUtil.getResult(dimTypeEnum,"AA3"),AccountDimTypeUtil.getResult(dimTypeEnum,"Js"),DIV_SCALE),new BigDecimal(100));
                break;
            case 绩效和计价:
                result = BigDecimalUtils.mul(BigDecimalUtils.div(AccountDimTypeUtil.getResult(dimTypeEnum,"AA4"),AccountDimTypeUtil.getResult(dimTypeEnum,"Js"),DIV_SCALE),new BigDecimal(100));
                break;
            case 涉贷利息差:
                result = BigDecimalUtils.mul(BigDecimalUtils.div(AccountDimTypeUtil.getResult(dimTypeEnum,"AA5"),AccountDimTypeUtil.getResult(dimTypeEnum,"Js"),DIV_SCALE),new BigDecimal(100));
                break;
            default:
                result = BigDecimal.ZERO;
        }
        return result;
    }
}
